/**
 * 
 */
package it.nola.soccer;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Classe che contiene tutte le interazioni con il db
 * 
 * @author paolo
 */
public class DbAdapter {
    private static class DatabaseHelper extends SQLiteOpenHelper {
	DatabaseHelper(Context context) {
	    super(context, DbConst.DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	    db.execSQL("create table "+DbConst.cronache.NAME+"(_id integer primary key autoincrement, "
		+DbConst.cronache.KEY_TITLE+" text not null, "+DbConst.cronache.KEY_BODY+" text not null, "
		+DbConst.cronache.KEY_TEAM1+" text not null, "+DbConst.cronache.KEY_ASSISTENT1+" text null, "
		+DbConst.cronache.KEY_REFEREE+" text not null, "+DbConst.cronache.KEY_RESULT+" text not null, "
		+DbConst.cronache.KEY_ASSISTENT2+" text null, "+DbConst.cronache.KEY_TEAM2+" text not null, "
		+DbConst.cronache.KEY_PLACE+" text not null default 'stadium', "+DbConst.cronache.KEY_MATCH_DATE
		+" text not null);");
	    db.execSQL("create table "+DbConst.players.NAME+"(_id integer primary key autoincrement, "
		+DbConst.players.KEY_CRONACA+" integer not null, "+DbConst.players.KEY_NR+" string not null, "
		+DbConst.players.KEY_NAME+" text not null, "+DbConst.players.KEY_TEAM+" integer not null);");
	    db.execSQL("create table "+DbConst.subst.NAME+"(_id integer primary key autoincrement, "
		+DbConst.subst.KEY_CRONACA+" integer not null, "+DbConst.subst.KEY_PLAYER_IN+" integer not null, "
		+DbConst.subst.KEY_PLAYER_OUT+" integer not null, "+DbConst.subst.KEY_MIN+" string not null, "
		+DbConst.subst.KEY_HALF+" integer not null, "+DbConst.subst.KEY_TEAM+" integer not null);");
	    db.execSQL("create table "+DbConst.actions.NAME+"(_id integer primary key autoincrement, "
		+DbConst.actions.KEY_CRONACA+" integer not null, "+DbConst.actions.KEY_DESCRI+" string not null, "
		+DbConst.actions.KEY_MIN+" string not null, "+DbConst.actions.KEY_HALF+" integer not null, "
		+DbConst.actions.KEY_TEAM+" integer null);");
	    db.execSQL("create table "+DbConst.cards.NAME+"(_id integer primary key autoincrement, "
		+DbConst.cards.KEY_CRONACA+" integer not null, "+DbConst.cards.KEY_PLAYER+" integer not null, "
		+DbConst.cards.KEY_MIN+" string not null, "+DbConst.cards.KEY_HALF+" integer not null, "
		+DbConst.cards.KEY_CARD+" integer not null,"+DbConst.cards.KEY_REASON+" string null,"
		+DbConst.cards.KEY_TEAM+" integer null);");
	    db.execSQL("create table "+DbConst.goals.NAME+"(_id integer primary key autoincrement, "
		+DbConst.goals.KEY_CRONACA+" integer not null, "+DbConst.goals.KEY_PLAYER+" integer not null, "
		+DbConst.goals.KEY_MIN+" string not null, "+DbConst.goals.KEY_IS_AUTO+" integer not null, "
		+DbConst.goals.KEY_HALF+" integer not null, "+DbConst.goals.KEY_TEAM+" integer null);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion
		+", which will destroy all old data");
	    // db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
	    // onCreate(db);
	}
    }

    public static final String KEY_TITOLI="TITOLI";
    private static final String TAG="NotesDbAdapter";
    private DatabaseHelper mDbHelper;

    private SQLiteDatabase mDb;

    private static final int DATABASE_VERSION=2;

    private final Context mCtx;

    /**
     * Il costruttore - prende il contesto per permettergli di creare / aprire il db
     * 
     * @param ctx : il contesto in cui lavorare
     */
    public DbAdapter(Context ctx) {
	this.mCtx=ctx;

    }

    public void aggiornamento() {
	// mDb.execSQL("create table "+DbConst.subst.NAME+"(_id integer primary key autoincrement, "
	// +DbConst.subst.KEY_CRONACA+" integer not null, "+DbConst.subst.KEY_PLAYER_IN
	// +" integer not null, "+DbConst.subst.KEY_PLAYER_OUT+" integer not null, "
	// +DbConst.subst.KEY_MIN+" string not null, "+DbConst.subst.KEY_HALF
	// +" integer not null, "+DbConst.subst.KEY_TEAM+" integer not null);");
	// mDb.execSQL("create table "+DbConst.actions.NAME+"(_id integer primary key autoincrement, "
	// +DbConst.actions.KEY_CRONACA+" integer not null, "+DbConst.actions.KEY_DESCRI
	// +" string not null, "+DbConst.actions.KEY_MIN+" string not null, "
	// +DbConst.actions.KEY_HALF+" integer not null, "+DbConst.actions.KEY_TEAM
	// +" integer null);");
	// mDb.execSQL("create table "+DbConst.cards.NAME+"(_id integer primary key autoincrement, "
	// +DbConst.cards.KEY_CRONACA+" integer not null, "+DbConst.cards.KEY_PLAYER
	// +" integer not null, "+DbConst.cards.KEY_MIN+" string not null, "
	// +DbConst.cards.KEY_REASON+" string null, "+DbConst.cards.KEY_HALF
	// +" integer not null, "+DbConst.cards.KEY_TEAM+" integer null);");
	mDb.execSQL("drop table cards");
	mDb.execSQL("create table "+DbConst.cards.NAME+"(_id integer primary key autoincrement, "
	    +DbConst.cards.KEY_CRONACA+" integer not null, "+DbConst.cards.KEY_PLAYER+" integer not null, "
	    +DbConst.cards.KEY_MIN+" string not null, "+DbConst.cards.KEY_HALF+" integer not null, "
	    +DbConst.cards.KEY_CARD+" integer not null,"+DbConst.cards.KEY_REASON+" string null,"
	    +DbConst.cards.KEY_TEAM+" integer null);");
	// mDb.execSQL("ALTER table cronache ADD COLUMN "+DbConst.cronache.KEY_PLACE+" text not null default 'stadium'");
	// mDb.execSQL("drop table players");
	// mDb.execSQL("create table actions (_id integer primary key autoincrement, cronaca integer not null, team text not null,  min text not null, tempo text not null, descri text)");
	// mDb.execSQL("create table cards(_id integer primary key autoincrement, cronaca text not null,  nr integer not null, min text not null, tempo text not null, descri text)");
    }

    /**
     * @param cronaca : la cronaca da controllare
     * @return true se ci sono giocatori per entrambe le squadre, false altrimenti
     */
    public boolean areThereAnyPlayer(Long cronaca) {
	if (cronaca==null) {
	    return false;
	}
	Cursor c=mDb.query(DbConst.players.NAME, new String[]{DbConst.players.KEY_ROWID }, DbConst.players.KEY_CRONACA
	    +"="+cronaca+" AND "+DbConst.players.KEY_TEAM+"="+DbConst.HOME_TEAM, null, null, null, null);
	if (!c.moveToFirst()) {
	    c.close();
	    return false;
	}
	c=mDb.query(DbConst.players.NAME, new String[]{DbConst.players.KEY_ROWID }, DbConst.players.KEY_CRONACA+"="
	    +cronaca+" AND "+DbConst.players.KEY_TEAM+"="+DbConst.FOREIGN_TEAM, null, null, null, null);
	boolean ret=c.moveToFirst();
	c.close();
	return ret;
    }

    public void cancellaGoal(long rowId) {
	mDb.delete(DbConst.goals.NAME, DbConst.goals.KEY_ROWID+"="+rowId, null);
    }

    public void cancellaSubst(long rowId) {
	mDb.delete(DbConst.subst.NAME, DbConst.subst.KEY_ROWID+"="+rowId, null);
    }

    public Cursor cercaGoal(long cronaca) {
	return mDb.rawQuery("SELECT g._id AS "+DbConst.goals.KEY_ROWID+", p."+DbConst.players.KEY_NAME+" AS "
	    +DbConst.goals.KEY_NAME_PLAYER+", g."+DbConst.goals.KEY_TEAM+" AS "+DbConst.goals.KEY_TEAM+", "
	    +DbConst.goals.KEY_HALF+", "+DbConst.goals.KEY_IS_AUTO+","+DbConst.subst.KEY_MIN+" FROM "
	    +DbConst.goals.NAME+" AS g JOIN "+DbConst.players.NAME+" AS p ON "+DbConst.goals.KEY_PLAYER+"=p."
	    +DbConst.players.KEY_ROWID+" WHERE g."+DbConst.players.KEY_CRONACA+"="+cronaca+" ORDER BY g."
	    +DbConst.goals.KEY_ROWID+" ASC", null);
    }

    /**
     * @param squadra : 1 (squadra di casa) o 2 (squadra in trasferta)
     * @param cronaca : l'id della cronaca per la quale cerchiamo la formazione
     * @return un cursore che contiente DbConst.players.MAX_PLAYERS righe
     */
    public Cursor cercaSquadra(short squadra, long cronaca) {
	return mDb.query(DbConst.players.NAME, DbConst.players.ALL_FIELDS, DbConst.players.KEY_CRONACA+"="+cronaca
	    +" AND "+DbConst.players.KEY_TEAM+"="+squadra, null, null, null, DbConst.players.KEY_ROWID+" ASC");
    }

    public Cursor cercaSubst(long cronaca) {
	return mDb.rawQuery("SELECT s._id AS "+DbConst.subst.KEY_ROWID+", team_in."+DbConst.players.KEY_NAME+" AS "
	    +DbConst.subst.KEY_NAME_PLAYER_IN+", team_out."+DbConst.players.KEY_NAME+" AS "
	    +DbConst.subst.KEY_NAME_PLAYER_OUT+", "+DbConst.subst.KEY_HALF+","+DbConst.subst.KEY_MIN+" FROM ("
	    +DbConst.subst.NAME+" AS s JOIN "+DbConst.players.NAME+" AS team_in ON "+DbConst.subst.KEY_PLAYER_IN
	    +"=team_in."+DbConst.players.KEY_ROWID+") JOIN "+DbConst.players.NAME+" AS team_out ON "
	    +DbConst.subst.KEY_PLAYER_OUT+"=team_out."+DbConst.players.KEY_ROWID+" WHERE s."
	    +DbConst.players.KEY_CRONACA+"="+cronaca+" ORDER BY s."+DbConst.subst.KEY_ROWID+" ASC", null);
    }

    public void close() {
	mDbHelper.close();
    }

    /**
     * Crea una nuova cronaca usando le informazioni passate. Se la nota è creata con successo the
     * note is successfully created return the new rowId for that note, otherwise return a -1 to
     * indicate failure.
     * 
     * @param title the title of the note
     * @param notes the body of the note
     * @param result
     * @param referee
     * @param assistent2
     * @param assistent1
     * @param place
     * @return rowId or -1 if failed
     */
    public long createNote(String title, String notes, String team1, String team2, String md, String assistent1,
	String assistent2, String referee, String result, String place) {
	ContentValues args=new ContentValues();
	args.put(DbConst.cronache.KEY_TITLE, title);
	args.put(DbConst.cronache.KEY_BODY, notes);
	args.put(DbConst.cronache.KEY_MATCH_DATE, md);
	args.put(DbConst.cronache.KEY_TEAM1, team1);
	args.put(DbConst.cronache.KEY_TEAM2, team2);
	args.put(DbConst.cronache.KEY_ASSISTENT1, assistent1);
	args.put(DbConst.cronache.KEY_ASSISTENT2, assistent2);
	args.put(DbConst.cronache.KEY_REFEREE, referee);
	args.put(DbConst.cronache.KEY_RESULT, result);
	args.put(DbConst.cronache.KEY_PLACE, place);
	return mDb.insert(DbConst.cronache.NAME, null, args);
    }

    /**
     * Cancella una nota con chiave rowId
     * 
     * @param rowId : id della cronaca da cancellare
     * @return true se cancellata, false altrimenti
     */
    public boolean deleteNote(long rowId) {
	return mDb.delete(DbConst.cronache.NAME, DbConst.cronache.KEY_ROWID+"="+rowId, null)>0;
    }

    /**
     * Restituisce un Cursor che si muove sulla lista di tutte le cronache in db
     * 
     * @return Cursor delle cronache
     */
    public Cursor fetchAllNotes() {
	return mDb.query(DbConst.cronache.NAME, DbConst.cronache.ALL_FIELDS, null, null, null, null, null);
    }

    public Cursor fetchAllNotes(Long id) {
	return mDb.query(DbConst.cronache.NAME, DbConst.cronache.ALL_FIELDS, DbConst.cronache.KEY_ROWID+"="+id, null,
	    null, null, null);
    }

    /**
     * Resituisce un elenco di titoli
     * 
     * @return
     */
    public Cursor fetchAllTitles() {
	return mDb.rawQuery("SELECT "+DbConst.cronache.KEY_ROWID+", "+DbConst.cronache.KEY_TEAM1+"|| ' - ' || "
	    +DbConst.cronache.KEY_TEAM2+" || ' ' || "+DbConst.cronache.KEY_RESULT+" AS "+KEY_TITOLI+" FROM "
	    +DbConst.cronache.NAME+" ORDER BY "+DbConst.cronache.KEY_ROWID+" DESC", null);
    }

    /**
     * Restituisce un Cursore posizione alla cronaca con il dato rowId
     * 
     * @param rowId id della cronaca da restituire
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchNote(long rowId) throws SQLException {
	Cursor mCursor=mDb.query(true, DbConst.cronache.NAME, DbConst.cronache.ALL_FIELDS, DbConst.cronache.KEY_ROWID
	    +"="+rowId, null, null, null, null, null);
	if (mCursor!=null) {
	    mCursor.moveToFirst();
	}
	return mCursor;
    }

    /**
     * @param cronaca : la cronaca cui si fa riferimento
     * @param squadra : 1 o 2
     * @return un cursore con la coppia _id - (nr + nome del giocatore)
     */
    public Cursor getAllPlayers(long cronaca, int squadra) {
	return mDb.rawQuery("Select "+DbConst.players.KEY_ROWID+", "+DbConst.players.KEY_NR+"||' - '||"
	    +DbConst.players.KEY_NAME+" AS "+DbConst.players.KEY_NAME+" FROM "+DbConst.players.NAME+" WHERE "
	    +DbConst.players.KEY_CRONACA+"="+cronaca+" AND "+DbConst.players.KEY_TEAM+"="+squadra+" ORDER BY "
	    +DbConst.players.KEY_NR+" ASC", null);
    }

    public void insertGoal(long player, int team, int half, String min, long cronaca, boolean isAuto) {
	ContentValues args=new ContentValues();
	int isAutoNum=isAuto ? 1 : 0;
	args.put(DbConst.goals.KEY_PLAYER, player);
	args.put(DbConst.goals.KEY_IS_AUTO, isAutoNum);
	args.put(DbConst.goals.KEY_CRONACA, cronaca);
	args.put(DbConst.goals.KEY_HALF, half);
	args.put(DbConst.goals.KEY_MIN, min);
	args.put(DbConst.goals.KEY_TEAM, team);
	mDb.insert(DbConst.goals.NAME, null, args);
    }

    /**
     * Inserimento di una sostituzione
     * 
     * @param player_in
     * @param player_out
     * @param team
     * @param half
     * @param min
     * @param cronaca
     */
    public void insertSubst(long player_in, long player_out, int team, int half, String min, long cronaca) {
	ContentValues args=new ContentValues();
	args.put(DbConst.subst.KEY_PLAYER_IN, player_in);
	args.put(DbConst.subst.KEY_PLAYER_OUT, player_out);
	args.put(DbConst.subst.KEY_CRONACA, cronaca);
	args.put(DbConst.subst.KEY_HALF, half);
	args.put(DbConst.subst.KEY_MIN, min);
	args.put(DbConst.subst.KEY_TEAM, team);
	mDb.insert(DbConst.subst.NAME, null, args);
    }

    /**
     * Open the notes database. If it cannot be opened, try to create a new instance of the
     * database. If it cannot be created, throw an exception to signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public DbAdapter open() throws SQLException {
	mDbHelper=new DatabaseHelper(mCtx);
	mDb=mDbHelper.getWritableDatabase();
	return this;
    }

    public Cursor recuperaGoal(long id) {
	return mDb.query(DbConst.goals.NAME, DbConst.goals.ALL_FIELDS, DbConst.goals.KEY_ROWID+"="+id, null, null,
	    null, null);
    }

    public Cursor recuperaSubst(long id) {
	return mDb.query(DbConst.subst.NAME, DbConst.subst.ALL_FIELDS, DbConst.subst.KEY_ROWID+"="+id, null, null,
	    null, null);
    }

    public String recuperaTitolo(long id) {
	Cursor c=mDb.rawQuery("SELECT "+DbConst.cronache.KEY_TEAM1+"|| ' - ' || "+DbConst.cronache.KEY_TEAM2
	    +" || ' ' || "+DbConst.cronache.KEY_RESULT+" AS "+KEY_TITOLI+" FROM "+DbConst.cronache.NAME, null);
	if (c.moveToFirst()) {
	    String ret=c.getString(c.getColumnIndex(KEY_TITOLI));
	    c.close();
	    return ret;
	}
	c.close();
	return null;
    }

    public Cursor riempiSqadra(short squadra, long cronaca) {
	for (int i=1; i<=DbConst.players.MAX_NUMBER; i++) {
	    ContentValues args=new ContentValues();
	    args.put(DbConst.players.KEY_CRONACA, cronaca);
	    args.put(DbConst.players.KEY_NAME, "");
	    if (i!=DbConst.players.MAX_NUMBER) {
		args.put(DbConst.players.KEY_NR, i);
	    }
	    else {
		args.put(DbConst.players.KEY_NR, "Mr.");
	    }
	    args.put(DbConst.players.KEY_TEAM, squadra);
	    mDb.insert(DbConst.players.NAME, null, args);
	}
	Cursor ret=cercaSquadra(squadra, cronaca);
	ret.moveToFirst();
	return ret;
    }

    public void updateGoal(long rowId, long player, int team, int half, String min, long cronaca, boolean isAuto) {
	ContentValues args=new ContentValues();
	int isAutoNum=isAuto ? 1 : 0;
	args.put(DbConst.goals.KEY_CRONACA, cronaca);
	args.put(DbConst.goals.KEY_PLAYER, player);
	args.put(DbConst.goals.KEY_IS_AUTO, isAutoNum);
	args.put(DbConst.goals.KEY_TEAM, team);
	args.put(DbConst.goals.KEY_HALF, half);
	args.put(DbConst.goals.KEY_MIN, min);
	mDb.update(DbConst.goals.NAME, args, DbConst.goals.KEY_ROWID+"="+rowId, null);
    }

    /**
     * Aggiorna la cronaca usando i dettagli forniti. The note to be updated is specified using the
     * rowId, and it is altered to use the title and body values passed in
     * 
     * @param rowId id of note to update
     * @param title value to set note title to
     * @param body value to set note body to
     * @return true if the note was successfully updated, false otherwise
     */
    public boolean updateNote(long rowId, String title, String body, String date, String team1, String team2,
	String assistent1, String assistent2, String referee, String result, String luogo) {
	ContentValues args=new ContentValues();
	args.put(DbConst.cronache.KEY_TITLE, title);
	args.put(DbConst.cronache.KEY_BODY, body);
	args.put(DbConst.cronache.KEY_MATCH_DATE, date);
	args.put(DbConst.cronache.KEY_TEAM1, team1);
	args.put(DbConst.cronache.KEY_TEAM2, team2);
	args.put(DbConst.cronache.KEY_ASSISTENT1, assistent1);
	args.put(DbConst.cronache.KEY_ASSISTENT2, assistent2);
	args.put(DbConst.cronache.KEY_REFEREE, referee);
	args.put(DbConst.cronache.KEY_RESULT, result);
	args.put(DbConst.cronache.KEY_PLACE, luogo);
	return mDb.update(DbConst.cronache.NAME, args, DbConst.cronache.KEY_ROWID+"="+rowId, null)>0;
    }

    public void updatePlayer(long rowId, short squadra, long cronaca, String nr, String nome) {
	ContentValues args=new ContentValues();
	args.put(DbConst.players.KEY_CRONACA, cronaca);
	args.put(DbConst.players.KEY_NAME, nome);
	args.put(DbConst.players.KEY_NR, nr);
	args.put(DbConst.players.KEY_TEAM, squadra);
	mDb.update(DbConst.players.NAME, args, DbConst.players.KEY_ROWID+"="+rowId, null);
    }

    /**
     * Aggiornamento di una sostituzione
     * 
     * @param parseLong
     * @param player_in
     * @param player_out
     * @param team
     * @param half
     * @param min
     * @param cronaca
     */
    public void updateSubst(long rowId, long player_in, long player_out, int team, int half, String min, long cronaca) {
	ContentValues args=new ContentValues();
	args.put(DbConst.subst.KEY_CRONACA, cronaca);
	args.put(DbConst.subst.KEY_PLAYER_IN, player_in);
	args.put(DbConst.subst.KEY_PLAYER_OUT, player_out);
	args.put(DbConst.subst.KEY_TEAM, team);
	args.put(DbConst.subst.KEY_HALF, half);
	args.put(DbConst.subst.KEY_MIN, min);
	mDb.update(DbConst.subst.NAME, args, DbConst.subst.KEY_ROWID+"="+rowId, null);
    }

    public void cancellaCard(long rowId) {
	mDb.delete(DbConst.cards.NAME, DbConst.goals.KEY_ROWID+"="+rowId, null);
    }

    public void insertCard(long player, int team, int half, String min, long cronaca, String motivo, int card) {
	ContentValues args=new ContentValues();
	args.put(DbConst.cards.KEY_PLAYER, player);
	args.put(DbConst.cards.KEY_REASON, motivo);
	args.put(DbConst.cards.KEY_CRONACA, cronaca);
	args.put(DbConst.cards.KEY_HALF, half);
	args.put(DbConst.cards.KEY_MIN, min);
	args.put(DbConst.cards.KEY_TEAM, team);
	args.put(DbConst.cards.KEY_CARD, card);
	mDb.insert(DbConst.cards.NAME, null, args);

    }

    public void updateCard(long rowId, long player, int team, int half, String min, long cronaca, String motivo,
	int card) {
	ContentValues args=new ContentValues();
	args.put(DbConst.cards.KEY_CRONACA, cronaca);
	args.put(DbConst.cards.KEY_PLAYER, player);
	args.put(DbConst.cards.KEY_REASON, motivo);
	args.put(DbConst.cards.KEY_TEAM, team);
	args.put(DbConst.cards.KEY_HALF, half);
	args.put(DbConst.cards.KEY_MIN, min);
	args.put(DbConst.cards.KEY_CARD, card);
	mDb.update(DbConst.cards.NAME, args, DbConst.goals.KEY_ROWID+"="+rowId, null);
    }

    public Cursor cercaCard(long cronaca) {
	return mDb.rawQuery("SELECT c._id AS "+DbConst.cards.KEY_ROWID+", p."+DbConst.players.KEY_NAME+" AS "
	    +DbConst.cards.KEY_NAME_PLAYER+", c."+DbConst.cards.KEY_TEAM+" AS "+DbConst.cards.KEY_TEAM+", "
	    +DbConst.cards.KEY_HALF+", "+DbConst.cards.KEY_CARD+","+DbConst.subst.KEY_MIN+" FROM "+DbConst.cards.NAME
	    +" AS c JOIN "+DbConst.players.NAME+" AS p ON "+DbConst.cards.KEY_PLAYER+"=p."+DbConst.players.KEY_ROWID
	    +" WHERE c."+DbConst.players.KEY_CRONACA+"="+cronaca+" ORDER BY c."+DbConst.cards.KEY_ROWID+" ASC", null);
    }

    public Cursor recuperaCard(long id) {
	return mDb.query(DbConst.cards.NAME, DbConst.cards.ALL_FIELDS, DbConst.cards.KEY_ROWID+"="+id, null, null,
	    null, null);
    }
}
